package com.example.administrator.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ListView;

import com.android.volley.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import httptool.getHttpUrl;
import httptool.takeUrlResource;

public  class MainActivity1 extends AppCompatActivity{
    private ListView lv;
    private MeAdapter meadapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list1);
        lv = (ListView) findViewById(R.id.listview);
        getJson1();
    }
    private ArrayList<Map<String,String>> data = new ArrayList<Map<String,String>>();
    private void getJson1() {
        takeUrlResource.getJson(getApplicationContext(), getHttpUrl.index, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                try {
                    JSONObject jso = new JSONObject(s);
                    String st = jso.getString("retcode");
                    if (st.equals("200")){
                        JSONArray jsa = jso.getJSONArray("news");
                        for (int i = 0;i < jsa.length();i++){
                            JSONObject jons = jsa.getJSONObject(i);
                            String title  = jons.getString("title");
                            String writer = jons.getString("writer");
                            Log.e("这个是文章跟标题",title+"   "+writer);
                            Map<String,String> map = new HashMap<String ,String>();
                            map.put("title",title);
                            map.put("dec",writer);
                            JSONArray img = jons.getJSONArray("imgs");
                            for (int j = 0;j < img.length();i++){
                                String imgs = img.getString(j);
                                map.put("img",imgs);
                            }
                            data.add(map);
                        }
                        Log.e("这个是数据",data.size()+ "0000");
                        meadapter = new MeAdapter(getApplicationContext(),data);
                        lv.setAdapter(meadapter);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
